package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2023-02-13
 * Time: 14:54
 */
//此处保证Singleton这个类只能创建出一个实例
class Singleton{
    //在此处,先把这个实例给创建出来
    private static Singleton instance = new Singleton();
    //如果需要使用这个唯一实例,统一通过Singleton.getInstance()方式来获取
    public static Singleton getInstance(){
        return instance;
    }
    //为了避免Singleton类不小心被复制出多份
    //把构造方法设为private.在类外面,就无法通过new的方式来创建这个Singleton实例
    private Singleton(){

    }
}

public class Thread20 {
    public static void main(String[] args) {
        Singleton s = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s == s2);
    }

}
